package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.page.MyPage;
import com.itheima.entity.YddCate;
import com.itheima.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {
    @Autowired
    private YddCateService cateService;

    /*
     * 新增分类
     * */
    @PostMapping("/add")
    public R<String> save(@RequestBody YddCate yddCate) {
        log.info("yddCate: {}", yddCate);
        cateService.save(yddCate);
        return R.success("新增分类成功");
    }

    @GetMapping("/list")
    public R<Object> page(String catename, int page, int limit) {
        //构造分页构造器
        //PageInfo<YddCate> pageInfo = new PageInfo<>(page,limit);
        // PageInfo <YddCate> pageInfo=new PageInfo<YddCate>(page,limit)
        //Page<YddCate> pageInfo = new Page<YddCate>(page, limit);
//        PageHelper.startPage(page,limit);
        Page<YddCate> yddCatePage = new Page<>(page, limit);
        //构造条件构造器
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        //添加个排序条件
        queryWrapper.orderByDesc(YddCate::getDefaultData);
        //执行查询
        //List<YddCate> list = cateService.list(queryWrapper);
        cateService.page(yddCatePage,queryWrapper);
        MyPage<YddCate> myPage = new MyPage<>();
        myPage.setCount(yddCatePage.getTotal());
        //每页条数
        myPage.setPageSize(yddCatePage.getSize());
        //总页数
        myPage.setTotalPages((yddCatePage.getTotal() / yddCatePage.getSize()) + 1);
        //当前页
        myPage.setCurrentPage(yddCatePage.getCurrent());
        myPage.setData(yddCatePage.getRecords());
       // PageBean<YddCate> yddCateDto = new PageBean<>(list);
        return R.success(myPage);

    }

    @GetMapping("/rowInfo")
    public R<YddCate> rowInfo(Integer id) {
        YddCate yddCate = cateService.getById(id);
        if (yddCate != null) {
            return R.success(yddCate);
        }
        return R.error("无数据", 1000);
    }

    /*根据ID修改分类*/
    @PostMapping("/update")
    public R<String> update(@RequestBody YddCate yddCate) {
        log.info("修改分类信息: {}", yddCate);
        cateService.updateById(yddCate);
        return R.success("修改分类信息成功了");
    }

    @GetMapping("/del")
    public R<String> delete(Integer id) {
        cateService.remove(id);
        return R.success("分类信息删除成功!");

    }

    /**
     * 根据条件查询套餐数据
     * @param yddCate
     * @return
     */
    @GetMapping("/all")
    public R<List<YddCate>> list(YddCate yddCate) {
         //构造查询条件对象;
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(yddCate.getId() != null, YddCate::getId, yddCate.getId());
        queryWrapper.eq(YddCate::getDefaultData, 1);
        //添加排序条件
        queryWrapper.orderByAsc(YddCate::getSortNum).orderByDesc(YddCate::getCatename);
        //
        List<YddCate> list = cateService.list(queryWrapper);

        return R.success(list);

    }


}



